
//
//  NewsTableViewCell.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "NewsTableViewCell.h"
#import "NewsModel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"

#define kLabelWidth ([UIScreen mainScreen].bounds.size.width * 0.6)
#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface NewsTableViewCell ()

@property (nonatomic, strong)UIView *backView;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *commentLabel;
@property (nonatomic, strong)UILabel *synopsisLabel;
@property (nonatomic, strong)UIImageView *headImageView;

@end
@implementation NewsTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _backView = [[UIView alloc]init];
        _backView.layer.borderColor = [UIColor grayColor].CGColor;
        _backView.layer.masksToBounds = YES;
        _backView.layer.cornerRadius = 10;
        _backView.layer.borderWidth = 1;
        [self.contentView addSubview:_backView];
        
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [self.backView addSubview:_titleLabel];
        
        _commentLabel = [[UILabel alloc]init];
        _commentLabel.font = [UIFont systemFontOfSize:12];
        _commentLabel.textColor = [UIColor grayColor];
        [self.backView addSubview:_commentLabel];
        
        _headImageView = [[UIImageView alloc]init];
        [self.backView addSubview:_headImageView];
        
        _synopsisLabel = [[UILabel alloc] init];
        _synopsisLabel.numberOfLines = 0;
        _synopsisLabel.textColor = [UIColor grayColor];
        _synopsisLabel.font = [UIFont systemFontOfSize:14];
        [self.backView addSubview:_synopsisLabel];

    }
    return self;
}

- (void)setModel:(NewsModel *)model
{
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.mas_equalTo(5);
        make.right.and.bottom.mas_equalTo(-5);
    }];
    
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.mas_equalTo(5);
        make.right.mas_equalTo(-kScreenWidth *0.7);
        make.bottom.mas_equalTo(-5);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).offset(5);
        make.top.mas_equalTo(5);
        make.right.mas_equalTo(-5);
        make.bottom.equalTo(self.headImageView.mas_top).offset(30);
    }];
    
    [self.synopsisLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).offset(5);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(0);
        make.right.mas_equalTo(-5);
        make.bottom.mas_equalTo(-5);
    }];
    
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headImageView.mas_right).offset(kScreenWidth * 0.5);
        make.top.equalTo(self.backView.mas_bottom).offset(-25);
        make.right.mas_equalTo(-5);
        make.bottom.mas_equalTo(-5);
    }];
    
    if (_model != model) {
        NSString *imageSandboxPath = nil;
        if (model.headImage == nil) {
            [self.headImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"picholder"]];
        }else
        {
            self.headImageView.image = [UIImage imageNamed:imageSandboxPath];
        }
        
        
        self.titleLabel.text = model.title;
        self.synopsisLabel.text = model.digest;
        
        // 如果回复太多就改成几点几万
        CGFloat count =  [model.replyCount intValue];
        NSString *displayCount = nil;
        if (count > 10000) {
            displayCount = [NSString stringWithFormat:@"%.1f万跟帖",count/10000];
        }else{
            displayCount = [NSString stringWithFormat:@"%.0f跟帖",count];
        }
        self.commentLabel.text = displayCount;
        
    }

}

+ (instancetype)addNewsCellWithTableView:(UITableView *)tableView model:(NewsModel *)model
{
    NSString *cellIdentifier = @"NewsCell";
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[NewsTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellIdentifier];
    }
    cell.model = model;
    return cell;

}


@end
